//
//  LTY_MusicProjectViewController.m
//  MooMusic
//
//  Created by 守护神 on 15/10/21.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "LTY_MusicProjectViewController.h"
// 导入自定义cell的头文件
#import "LTY_MusicProjectTableViewCell.h"
// 导入WKWebview的头文件
#import "LTY_WKWebViewController.h"
// 导入webviewcontroller的头文件
#import "LTY_WebViewController.h"
// 导入network头文件
#import "NetWork.h"
// 导入音乐专题model的头文件
#import "LTY_MusicProjectModel.h"
// 导入摇一摇头文件
#import "LTY_ShakeViewController.h"

@interface LTY_MusicProjectViewController ()<UITableViewDelegate, UITableViewDataSource>
// 创建一个音乐专题的tableview
@property (nonatomic, strong)UITableView *projectTableView;
// 定义一个数据源
@property (nonatomic, strong)NSMutableArray *musicDataArray;

@end

static NSString *const cellIndertifer = @"cellIndertifer";

@implementation LTY_MusicProjectViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.musicDataArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"音乐专题";
    self.view.backgroundColor = [UIColor whiteColor];
    
    //摇一摇图标
    UIImage *shakeImage = [UIImage imageNamed:@"iconfont_yaoyiyao.png"];
    // 取消渲染
    shakeImage = [shakeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:shakeImage style:UIBarButtonItemStylePlain target:self action:@selector(shakeButton)];
    
    // 创建视图
    [self creatMusicProjectTableview];
    // 获取数据
    [self getMusisData];
    
    // Do any additional setup after loading the view.
}

// 获取数据
- (void)getMusisData
{
    // 网络请求
    [NetWork networkGETRequestWithURL:musicProject Paramater:nil ResultBlock:^(id result) {
        
        NSMutableArray *tempArray = [NSMutableArray array];
        tempArray = [result objectForKey:@"data"];
        
        self.musicDataArray = [LTY_MusicProjectModel arrayWithModelByArray:tempArray];
        
        // 刷新tableview
        if (self.musicDataArray.count > 0) {
            [self.projectTableView reloadData];
        }
        
        
    }];
}

// 创建音乐专题的视图
- (void)creatMusicProjectTableview
{
    self.projectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
    self.projectTableView.backgroundColor = [UIColor whiteColor];
    self.projectTableView.delegate = self;
    self.projectTableView.dataSource = self;
    // 注册
    [self.projectTableView registerClass:[LTY_MusicProjectTableViewCell class] forCellReuseIdentifier:cellIndertifer];
    [self.view addSubview:self.projectTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTY_MusicProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndertifer];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.musicDataArray.count > 0) {
        cell.mdoel = [self.musicDataArray objectAtIndex:indexPath.row];
    }
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 版本判断 wkwebview不支持8.0以下的
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
        LTY_WKWebViewController *WKWebVC = [[LTY_WKWebViewController alloc] init];
        WKWebVC.WkwebString = [[self.musicDataArray objectAtIndex:indexPath.row] valueForKey:@"actionUrl"];
        [self.navigationController pushViewController:WKWebVC animated:YES];
    }else{
        
        LTY_WebViewController *WebVC = [[LTY_WebViewController alloc] init];
        WebVC.webString = [[self.musicDataArray objectAtIndex:indexPath.row] valueForKey:@"actionUrl"];
        [self.navigationController pushViewController:WebVC animated:YES];
        
    }
    
    
    
    
    
}

#pragma mark -- 摇一摇button触发的方法
- (void)shakeButton
{
    NSLog(@"摇一摇");
    LTY_ShakeViewController *shake = [[LTY_ShakeViewController alloc] init];
//        UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:shake];
    [shake.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    shake.navigationItem.title = @"摇一摇";
    [self.navigationController pushViewController:shake animated:YES];
//    [self presentViewController:navc animated:YES completion:^{
//        
//    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
